package com.itheima.reggie.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.UUID;

@RestController
@RequestMapping("alipay")
public class PayController {


	private final String APP_ID = "2021000120611475";
	private final String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCKb/5qUUX6/fqnnw6DlwoH8m24Zqv0W8AJd7O19xh9MxltzEYBpa0FYgA3GJH4R1ZggIqA9PCRnSy8tuBam6MF/jAUer/5u9lCqo90zHRvHgy6U+CY6EQ31DlykOL8IXj6jIC5KXhPz8/bvKY1mytKelKY4xPtKVRDE9C8M0U/KaiSWD3P2Kq1BpTdTTaxZa+JULQz8V9YCv+hI5TuNqPcoW0WuHCY8VWj4cjB1ZIjRwMUeZrT2V4oVldMProv/23qJh/Dphtdx9P4fqGe8BstnwZnLNI+ga6q7a1BcpH7p4Uq1mh4eqhZ9Nx7PSMrQqlIz7FB1o0f2BB+ja/sgLfJAgMBAAECggEAdjSQgQlqfpjUqMGoa/XQq951j0mjs9+PxsW93jbhYy35nYYZlJh5dyop0uGY6e1HtNis6tW6gCs7ottoEBXqilhzh1kUiNG7/i+SlIFrPeHjrkmmcgmJDj6Y2tkZ531RRmYoF3Jwd32TFyYqqYUX3t05n6/tOMjcWPYXeksw6m+FT4+CGykAL0yzUfREhfjaSGjgkHJp9Gal5ocppAiGH7wIrBEty55FrXb101SVZkZ/BpFOIi8VIsJs2vhMf5JF1fkzfkLxYW0HsbY1VQ6jVHhCTG1YyxjTyFyB9BrwcRl/QGHkaiyPxQE2Gu5rgkMhzvXg+IlErVoEmzSHLzfYcQKBgQDxFf+g2kS7USP6fGZJvr4GEh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pmIELCzMdM5lPYUgLgXf6U3RhpWaLUMhQXQBfLna0bXekjh8P2XhSyo1D0HDATzfBRELWQQ1DrwQv3nWlJJGW9abLSq6OqJcOwZqXig9pCSdSaIynk1ZTt9UQ4lSuu6dFBv4Fl9DaPbRJ19H2SIsooTmJCH27OApc+4CSU7TIyn+D02mkcbJumOIigMfQkPuRqaLN7e39D0fmwIDAQAB";
	//这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
	private final String GATEWAY_URL ="https://openapi.alipaydev.com/gateway.do";
	private final String FORMAT = "JSON";
	//签名方式
	private final String SIGN_TYPE = "RSA2";
	//支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
	private final String NOTIFY_URL = "http://127.0.0.1/notifyUrl";
	//支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
	private final String RETURN_URL = "http://127.0.0.1/returnUrl";



	@PostMapping
	public void alipay(HttpServletResponse httpResponse) throws IOException {

		SecureRandom r= new SecureRandom();
		//实例化客户端,填入所需参数
		AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
		AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
		//在公共参数中设置回跳和通知地址
		request.setReturnUrl(RETURN_URL);
		request.setNotifyUrl(NOTIFY_URL);

		//商户订单号，商户网站订单系统中唯一订单号，必填
		//生成随机Id
		String out_trade_no = UUID.randomUUID().toString();
		//付款金额，必填
		String total_amount =Integer.toString(648);
		//订单名称，必填
		String subject ="奥迪A8 2021款 A8L 60 TFSl quattro豪华型";
		//商品描述，可空
		String body = "尊敬的会员欢迎购买奥迪A8 2021款 A8L 60 TFSl quattro豪华型";
		request.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
				+ "\"total_amount\":\""+ total_amount +"\","
				+ "\"subject\":\""+ subject +"\","
				+ "\"body\":\""+ body +"\","
				+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
		String form = "";
		try {
			form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		httpResponse.setContentType("text/html;charset=" + CHARSET);
		httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面
		httpResponse.getWriter().flush();
		httpResponse.getWriter().close();
	}


}

